<?php

namespace Mall\Model;

use Common\Model\BaseModel;
use Common\Model\ModelException;
use Util\ModelUtils;

class GoodsModel extends BaseModel
{
    protected $_auto = [
        ['create_time', 'time', self::MODEL_INSERT, 'function'],
    ];

    protected $_validate = [
    ];

    protected $_scope = [
        'valid' => [
            'where' => ['audit' => 1, 'closed' => 0, 'delete_time' => 0],
        ]
    ];

    public function isValid($goodsList)
    {
        foreach ($goodsList as $goods) {
            if ($goods['closed'] == 1 || $goods['audit'] != '1') {
                return false;
            }
        }
        return true;
    }

    public function addComment($id)
    {
        D('Goods')->where(['goods_id' => $id])->setInc('comments_count', 1);
        return true;
    }

    public function calcTotalPrice($shop, $goodsList)
    {
        /*注意该系统并没有结算价*/
        $total = 0;
        // $settlementTotal = 0;
        foreach ($goodsList as $goods) {
            $total += $goods['price'] * $goods['quantity'];
            // $settlementTotal += $goods['settlement_price'] * $goods['quantity'];
        }
        
    }

    protected function _after_find(&$record, $options)
    {
        $user = get_user();
      
        if ($record['photo']) $record['photo'] = explode(',', $record['photo']);
        if ($record['details']) $record['details'] = html_entity_decode($record['details']);
        $collected=M('goodsCollected')->where(['user_id'=>$user['user_id'],'goods_id'=>$record['goods_id']])->find();
        $record['collected']=$collected;
        // $record['monthly_sales_count'] =  $redis->zscore('monthly_sales:' . date('Ym'), $record['goods_id'])?: '0';
        
    }

    public function fillGoods(&$goods)
    {
        $user = get_user();
        $attrs = D('GoodsAttr')->where(['goods_id' => $goods['goods_id'], 'status' => '1'])->select();
        $attrs = list_keep($attrs, ['id', 'name', 'val']);


        $specs = D('GoodsSpec')->where(['goods_id' => $goods['goods_id'], 'status' => '1'])->select();
        $specs = array_group_by($specs, 'name');
        foreach ($specs as &$spec) {
            $spec['items'] = list_keep($spec['items'], ['id', 'val']);
        }

        $skus = D('GoodsSku')->field('name,price,wholesale_price,stock')->where(['goods_id' => $goods['goods_id']])->select();
        // 售价判断
        foreach ($skus as &$sku) {
            $sku['price'] = $user['role'] === '2' ? $sku['wholesale_price'] : $sku['price'];
            unset($sku['wholesale_price']);
        }
        $goods['attrs'] = $attrs;
        $goods['specs'] = $specs;
        $goods['skus'] = $skus;
    }

    public function fillGoodsFromSku(&$goodssGroup)
    {
        $user = get_user();
        foreach ($goodssGroup as &$itemGroup) {
            foreach ($itemGroup as &$goods) {
                $sku = unserialize($goods['sku']);
                $goods['selectedSpecs'] = array_values($sku['items']);
                $goods['selectedSku'] = array_keep($sku, ['id', 'name', 'price','stock']);
               
            }
        }
    }
    

    protected function _after_select(&$records, $options)
    {
        // $redis = redis();
        $user = get_user();
        foreach ($records as &$record) {
           
            if ($record['photo']) $record['photo'] = explode(',', $record['photo']);
            if ($record['details']) $record['details'] = html_entity_decode($record['details']);
            // $record['monthly_sales_count'] = $redis->zscore('monthly_sales:' . date('Ym'), $record['goods_id']) ?: '0';
        }
    }
    
}
